using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class JobSeekers_resume : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string resumeID = Request.QueryString.Get("ResumeID");
        try
        {
            connectionString =
                 WebConfigurationManager.ConnectionStrings[
                   "RecruitVietnam"].ConnectionString;
            if (Session.Contents["jsuserid"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                
                txtContactEmail.Text = 
                Convert.ToString(Session["jsemail"]);
                
                if(resumeID == null) resumeID ="";
                    
                if (!IsPostBack)
                {
                    LoadDataForDropDownList();
                    EditResume(resumeID);
                }
            }
            if (resumeID == null || resumeID == "")
            {
                Common cls = new Common(connectionString);
                if (cls.CheckResume(Convert.ToString(
                Session.Contents["jsuserid"])) >= 5)
                    btnNext.Enabled = false;
                else
                    btnNext.Enabled = true;
            }
        }
        catch (Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }
    void LoadDataForDropDownList()
    {
        Common cls = new Common(connectionString);
        DataTable dataTable = cls.GetDataTable(       
        " select * from tblJSCertificates;");
        DropDownListHelper helper =
        new DropDownListHelper();
        helper.FillDataTableToDropDownList(
        dataTable,  ddlQualification,        
            "CertificateName",
           "CertificateID" ,"");
    }
    void EditResume(string resumeID)
    {
        if (resumeID != "" && resumeID != null)
        {
            SqlServerProvider cls = new 
            SqlServerProvider(connectionString);
            cls.CommandText = "SPViewResume";
            cls.CommandType = CommandType.StoredProcedure;
            cls.ParameterCollection = new 
            string[2] { "@UserID", "@ResumeID" };
            cls.ValueCollection = new 
            string[2] { 
            Session["jsuserid"].ToString(), resumeID };
            
            DataRow dataRow =
            cls.GetDataRow();
            if (dataRow != null)
            {
                ResumeID.Value = dataRow[0].ToString();            
                txtJob.Text = dataRow[2].ToString();
                txtContactEmail.Text = dataRow[3].ToString();
                txtObjective.Text=dataRow[4].ToString();
                txtSkills.Text = dataRow[5].ToString();
                ddlExperienceYear.SelectedValue = dataRow[6].ToString();
                ddlQualification.SelectedValue = dataRow[7].ToString();
                txtCurrentCompany.Text = dataRow[8].ToString();
                ddlViewable.SelectedValue = dataRow[9].ToString();
                ddlTemplate.SelectedValue = dataRow[11].ToString();
                if (dataRow[10].ToString() == "True")
                    rdbConfidential.Checked =  true ;
                else
                    rdbNotConfidential.Checked = true;
                btnNext.Text = "Update";
            }
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        try
        {
        RecruitVietnam cls = new RecruitVietnam();
        cls.ConnectionString = connectionString;
         
        int returnValue = cls.AddNewOrUpdateResume(
        ((ResumeID.Value != "") ? ResumeID.Value : "0"),
        Convert.ToString(Session.Contents["jsuserid"]),
        txtJob.Text, txtContactEmail.Text,
        txtObjective.Text,
        txtSkills.Text,
        ddlExperienceYear.SelectedValue,
        ddlQualification.SelectedValue,
        txtCurrentCompany.Text,
        ddlViewable.SelectedValue,
        ddlTemplate.SelectedValue,
        rdbConfidential.Checked);
        if (cls.ErrorMessage != "")            
            literalError.Text = "Error: " + cls.ErrorMessage;
        else
            {
                if(ResumeID.Value!="")
                    Session["ResumeID"] = ResumeID.Value;
                else
                    Session["ResumeID"]= returnValue;
                Response.Redirect("resumedetails.aspx");
            }
       }
       catch (Exception ex)
       {
           literalError.Text = ex.Message;
       }    
    }
}
